package com.cc.controller;

import com.cc.poko.Student;
import com.cc.service.StudentService;
import com.cc.utils.CommonsApi;
import com.google.gson.Gson;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author cc
 * @date 2021/8/11 20:50
 * @Description
 */
@WebServlet("/Student.do")
public class StudentController extends HttpServlet {
    StudentService studentService;

    public StudentController(StudentService studentService) {
        this.studentService = studentService;
    }

    public StudentController() {
    }

    public void setStudentService(StudentService studentService) {
        this.studentService = studentService;
    }

    @Override
    public void init() throws ServletException {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        studentService = context.getBean("studentService", StudentService.class);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");

        resp.setContentType("application/json;charset=utf-8");

        String method = req.getParameter("flag");
        CommonsApi commonsApi=null;
        Gson gson=new Gson();
        if ("add".equals(method)) {
            //新增
            String id = req.getParameter("id");
            String name = req.getParameter("name");
            String classid = req.getParameter("classid");
            String password = req.getParameter("password");
            //封装对象
            Student book=new Student(id,name,classid,password);
            int count = studentService.addstudent(book);
            if(count>0){
                commonsApi=new CommonsApi(200,"新增成功",null);
            }else{
                commonsApi=new CommonsApi(500,"新增失败",null);
            }
        }else if("all".equals(method)){
//            List<Book> books= bookService.findAll();
            String id = req.getParameter("id");

            Student findbyid = studentService.Findbyid(id);
            if(findbyid!=null){
                commonsApi=new CommonsApi(200,"查询成功",findbyid);
            }else{
                commonsApi=new CommonsApi(200,"请求成功,没有数据",findbyid);
            }
        }else if("update".equals(method)){
            String id = req.getParameter("id");
            String name = req.getParameter("name");
            String classid = req.getParameter("classid");
            String password = req.getParameter("password");
            //封装对象
            Student student=new Student(id,name,classid,password);
            int count = studentService.updateStudent(student);
            if(count>0){
                commonsApi=new CommonsApi(200,"更新成功",null);
            }else{
                commonsApi=new CommonsApi(500,"更新失败",null);
            }
        }else if("delete".equals(method)){
            String id = req.getParameter("id");
            int count = studentService.delectStudnet(id);
            if(count>0){
                commonsApi=new CommonsApi(200,"删除成功",null);
            }else{
                commonsApi=new CommonsApi(500,"删除失败",null);
            }
        }
        resp.getWriter().write(gson.toJson(commonsApi));
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }
}
